home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CD ROM Paradise Collection 4
/
CD ROM Paradise Collection 4 1995 Nov.iso
/
science
/
sm32a.zip
/
SYMBMATH.H10
< prev
next >
Wrap
Text File
|
1993-11-07
|
7KB
|
151 lines
3.2. Expressions
Expressions (i.e. expr) are made up of operators and operands.
Most operator are binary, that is, they take two operands; the rest
are unary and take only one operand. Binary operators use the usal
algebraic form, e.g. a+b.
There are two kinds of expressions: numeric and Boolean. The
numeric expression is combination of data and algebraic operators while
the Boolean expression is combination of data and relational operators
and logic operators. These two kinds of expressions can be mixed, but
the numeric expression has higher priority than Boolean operators.
x*(x>0) is different from x*x>0. x*x>0 is the same as (x*x)>0.
e.g.
a+b+3 numeric expression,
a>0 Boolean expression
a>0 and b>0 Boolean expression
(x>0)*x mixed numeric and Boolean expression
3.2.1 Operators
Table 3.2.1 Operators
-----------------------------------------------------------------
Operation Operators Examples Order
comma , a:=2, b:=3 1
assignment := p:=2+3 2
and and a>2 and a<8 2
or or a>2 or b>2 2
equation = x^2+x+1 = 0 3
equal == a==2 3
larger than > a>2 3
larger and equal >= a>=2 3
less than < a<2 3
less and equal <= a<=2 3
unequal <> a<>2 3
plus + a+b 4
minus - a-b 4
mutilation * a*b 5
division / a/b 5
power ^ a^b 6
power ** a**b 6
factorial ! n! 6
positive + +a 7
negative - -a 7
function f() sin(x) 7
list index f[] f[1] 7
parentheses () (a+b)*c 7
list [] [a,b] 7
--------------------------------------------------------------------
All functions have the same 7th order.
Operations with higher order precede, otherwise operations with
equal precedence are performed from left to right. These are the usual
algebraic conventions.
a^b^c is the same as (a^b)^c.
You can get operators by type(x).
3.2.1.1 Arithmetic operators
--------------------------------------------------------------------
plus + a+b 4
minus - a-b 4
mutilation * a*b 5
division / a/b 5
power ^ a^b 6
power ** a**b 6
positive + +a 7
negative - -a 7
--------------------------------------------------------------------
3.2.1.2 Relational Operators
Before you can write loops, you must be able to write
statements that evaluate to 1 or 0, and before you can do that, you
must be able to write useful statements with logical values. In
mathematics, these are relational statements.
SymbMath allows you to compare numbers six ways:
a < b less than
a <= b less than or equal to
a > b greater than
a >= b greater than or equal to
a == b equal to
a <> b not equals
SymbMath uses the double equals sign == (like C language) for
"is equal to" to distinguish this operator from the equation =.
The result of a comparison of two real numbers is either 1 or 0.
If the comparsion is not both real numbers, it left unevaluated.
3.2.1.3 Logical operators
SymbMath uses the logical operators: AND, and OR. You can
combine comparison operators with them to any level of complexity.
In contrast to Pascal, logical operators in SymbMath have a lower
order or precedence than the comparisons, so a < b and c > d
works as expected. The result of combining logical values with
logical operators is another logical value (1 or 0). Bit operations
on integers can be performed using the same operations, but result is
integers.
SymbMath uses the "short-circuit" definition of AND and OR
when the arguments are boolean. Here are tables that show how AND
and OR are defined:
a AND b
--------------------------------------------------------
b 1 0
a
1 1 0
0 0 0
------------------------------------------------------
a OR b
--------------------------------------------------------
b 1 0
a
1 1 1
0 1 0
------------------------------------------------------
Short-circuit evaluation is used because often one condition
must be tested before another is meaningful.
The result of Boolean expression with logic operators is either
1 or 0. Boolean expression like (1 < 3 or 1 > 4) return a real value 1
or 0. Numeric expressions can replace Boolean ones, provided
they evaluate to 1 or 0. The advantage here is that you can
define the step function that is 0 for x < a and 1 for x > a by
entering:
step(x_, a_) := x > a
To define the function:
f(x) = x-1 if x < 1
= x^2-x if x >= 1
enter:
f(x_) := (x-1)*(x < 1) + (x^2-x)*(x >= 1)
These functions can be differentiated and integrated symbolically.
3.2.2 Function calls
A function call activates the function specified by the function
name. The function call must have a list of actual parameters if the
corresponding function decalaration contains a list of formal parameters.
Each parameter takes the place of the corresponding formal parameter.
If the function is external, the function call will automatically load
the library specified by its function name plus extension .fun when needed.
Some examples of function calls follow:
sin(x) # will load the library sin.fun when needed
inte(x^2, x) # will load the library inte.fun when needed